package com.example.demo02.service;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo02.exception.ServiceException;
import com.example.demo02.mapper.EmployeeMapper;
import com.example.demo02.entity.Employee;
import com.example.demo02.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (Employee)表服务实现类
 *
 * @author makejava
 * @since 2024-09-02 23:50:33
 */
@Service("employeeService")
public class EmployeeService extends ServiceImpl<EmployeeMapper, Employee>{


    @Resource
    EmployeeMapper employeeMapper;

    @Override
    public boolean save(Employee entity) {
        if (StrUtil.isBlank(entity.getUsername())) {
            String name = entity.getName(); // 假设getName()方法获取员工姓名
            String phone = entity.getPhone(); // 假设getPhone()方法获取员工电话号码
            entity.setUsername(name + phone); // 将用户名设置为姓名和电话号码的组合
        }
        if(StrUtil.isBlank(entity.getPassword())){
            String phone = entity.getPhone();
            entity.setPassword(phone); //默认密码
        }
        return super.save(entity);
    }


    public Employee selectByUsername(String username) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);  //  eq => ==   where username = #{username}
        // 根据用户名查询数据库的用户信息
        return getOne(queryWrapper); //  select * from user where username = #{username}
    }

    public Employee selectByname(String name) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return getOne(queryWrapper);
    }

    // 验证用户账户是否合法
    public Employee login(Employee employee) {
        Employee dbEmp = selectByUsername(employee.getUsername());
        if (dbEmp == null) {
            // 抛出一个自定义的异常
            throw new ServiceException("用户名或密码错误");
        }
        if (!employee.getPassword().equals(dbEmp.getPassword())) {
            throw new ServiceException("用户名或密码错误");
        }
        // 生成token
        String token = TokenUtils.createToken(dbEmp.getId().toString(), dbEmp.getPassword());
        dbEmp.setToken(token);
        return dbEmp;
    }

    public Employee register(Employee employee) {
        Employee dbEmp = selectByUsername(employee.getUsername());
        if (dbEmp != null) {
            // 抛出一个自定义的异常
            throw new ServiceException("用户名已存在");
        }
        employee.setUsername(employee.getUsername());
        employeeMapper.insert(employee);
        return employee;
    }

    public void resetPassword(Employee employee) {
        Employee dbEmp = selectByUsername(employee.getUsername());
        if (dbEmp == null) {
            // 抛出一个自定义的异常
            throw new ServiceException("用户不存在");
        }
        if (!employee.getPhone().equals(dbEmp.getPhone())) {
            throw new ServiceException("验证错误");
        }
        dbEmp.setPassword("123");   // 重置密码
        updateById(dbEmp);
    }
}


